package com.keymozy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keymozy.mapper.MenuMapper;
import com.keymozy.mapper.PositionMapper;
import com.keymozy.pojo.Menu;
import com.keymozy.pojo.Position;
import com.keymozy.pojo.Staff;
import com.keymozy.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class IMenuService extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private PositionMapper positionMapper;

    @Override
    public List<Tree<String>> getMenu(Staff staff) {
        LambdaQueryWrapper<Menu> qw = new LambdaQueryWrapper<Menu>();
        List<Tree<String>> treeNodes=null;
//        获得所有父菜单
        qw.eq(Menu::getPid,0);
        qw.ne(Menu::getId,13);
        qw.ne(Menu::getId,14);
        List<Menu> menus = null;

//        获得职位权限
        Position position = positionMapper.selectById(staff.getPid());

        if ("-1".equals(position.getPrivil())){
            TreeNodeConfig config = new TreeNodeConfig();
            //id
            config.setIdKey("id");
            //父id
            config.setParentIdKey("pid");
            //名称
            config.setNameKey("rname");
            //最大递归深度
            config.setDeep(3);
            //孩子节点
            config.setChildrenKey("children");
            //排序字段
//            config.setWeightKey("id");
            treeNodes = TreeUtil.build(list(new LambdaQueryWrapper<Menu>()
                            .ne(Menu::getId,13).
                            ne(Menu::getId,14)), "0", config,
                    (treeNode, tree) -> {
                        tree.setId(treeNode.getId().toString());
                        tree.setParentId(treeNode.getPid().toString());
                        tree.setName(treeNode.getRname());
                        tree.putExtra("path", treeNode.getPath());
                        tree.putExtra("tap", treeNode.getTap());
                    });
        }else {
            List<Menu> ms = list(qw);
            menus = new ArrayList<>();
            for (Menu m : ms) {
                if (position.getPrivil().contains(m.getId()+"")){
                    menus.add(m);
                }
            }
        }
        return treeNodes;
    }

    @Override
    public List<Menu> listMenu() {
        List<Menu> fatherMenu = baseMapper.selectList(new LambdaQueryWrapper<Menu>().eq(Menu::getPid, 0));
        for (Menu menu : fatherMenu) {
            menu.setLabel(menu.getRname());
        }
        List<Menu> allMenu = this.list(new LambdaQueryWrapper<Menu>().ne(Menu::getPid, 0));
        for (Menu menu : allMenu) {
            menu.setLabel(menu.getRname());
        }
        List<Menu> menus = new ArrayList<>();
        for (Menu menu : fatherMenu) {
            menu.setLevel(1);
            menus.add(sortMenu(allMenu, menu));
        }
            return menus;
    }
    public Menu sortMenu(List<Menu> allMenu, Menu menu) {
        ArrayList<Menu> childMenu = new ArrayList<>();
        for (Menu m : allMenu) {
            if (menu.getId().equals(m.getPid())) {
                m.setLevel(menu.getLevel()+1);
                childMenu.add(m);
            }
        }
        if (childMenu.size() == 0) {
            return menu;
        } else {
            menu.setChildren(childMenu);
            for (Menu child : childMenu) {
                allMenu.remove(child);
                sortMenu(allMenu, child);
            }
            return menu;
        }
    }
}
